namespace cnVery.Cms
{
    using cnVery.Cms.Kernel;
    using cnVery.Utils;
    using cnVery.Data;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;
    using System.Web;
    using System.Web.UI.WebControls;

    public class categories
    {
        // Methods
        public static void Add(category model)
        {
            StringBuilder builder = new StringBuilder();
            int num = GetMax("sortid", "categories", "parentid=" + model.parentid.ToString()) + 1;
            model.sortid = num;
            builder.Append("insert into categories(");
            builder.Append("[parentid],[name],[description],[sortid]");
            builder.Append(")");
            builder.Append(" values (");
            builder.Append("" + model.parentid + ",");
            builder.Append("'" + model.name + "',");
            builder.Append("'" + model.description + "',");
            builder.Append("" + model.sortid + "");
            builder.Append(")");
            Connection connection = new Connection();
            connection.Execute(builder.ToString());
            connection.Close();
        }

        public static category[] Childs(category[] cs, int id)
        {
            int num = 0;
            for (int i = 0; i < cs.Length; i++)
            {
                if (cs[i].parentid == id)
                {
                    num++;
                }
            }
            if (num == 0)
            {
                return null;
            }
            category[] categoryArray = new category[num];
            int num3 = 0;
            for (int j = 0; j < cs.Length; j++)
            {
                if (cs[j].parentid == id)
                {
                    categoryArray[num3++] = cs[j];
                }
            }
            return categoryArray;
        }

        public static void Delete(int id)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete categories ");
            builder.Append(string.Concat(new object[] { " where id=", id, " or parentid=", id }));
            Connection connection = new Connection();
            connection.Execute(builder.ToString());
            connection.Close();
        }

        public static void Down(int id)
        {
            category model = GetModel(id);
            DataTable list = GetList("parentid=" + model.parentid.ToString() + " and sortid>" + model.sortid.ToString());
            if (list.Rows.Count >= 1)
            {
                category category2 = GetModel(int.Parse(list.Rows[0]["id"].ToString()));
                int sortid = model.sortid;
                model.sortid = category2.sortid;
                category2.sortid = sortid;
                Update(model);
                Update(category2);
            }
        }

        public static bool Exists(int id)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from categories where id=" + id + "");
            Connection connection = new Connection();
            object scalar = connection.GetScalar(builder.ToString());
            connection.Close();
            if (object.Equals(scalar, null) || object.Equals(scalar, DBNull.Value))
            {
                return false;
            }
            int num = (int)scalar;
            return (num > 0);
        }

        public static category[] GetCategories(DataTable dt)
        {
            category[] categoryArray = new category[dt.Rows.Count];
            for (int i = 0; i < categoryArray.Length; i++)
            {
                if (dt.Rows[i]["parentid"].ToString() != "")
                {
                    categoryArray[i].parentid = int.Parse(dt.Rows[i]["parentid"].ToString());
                }
                categoryArray[i].name = dt.Rows[i]["name"].ToString();
                categoryArray[i].description = dt.Rows[i]["description"].ToString();
                if (dt.Rows[i]["sortid"].ToString() != "")
                {
                    categoryArray[i].sortid = int.Parse(dt.Rows[i]["sortid"].ToString());
                }
            }
            return categoryArray;
        }

        public static DataTable GetChilds(int pid)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(" SELECT * FROM categories ");
            builder.Append(" WHERE (parentid = " + pid + ") OR (parentid IN (SELECT id ");
            builder.Append(" FROM categories ");
            builder.Append(" WHERE (parentid = " + pid + "))) ORDER BY sortid");
            Connection connection = new Connection();
            DataTable dataTable = connection.GetDataTable(builder.ToString());
            connection.Close();
            return dataTable;
        }

        public static DataTable GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from categories ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by sortid ");
            Connection connection = new Connection();
            DataTable dataTable = connection.GetDataTable(builder.ToString());
            connection.Close();
            return dataTable;
        }

        public static int GetMax(string field, string table, string where)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select max([" + field + "]) from [" + table + "] where " + where);
            Connection connection = new Connection();
            object scalar = connection.GetScalar(builder.ToString());
            connection.Close();
            if (object.Equals(scalar, null) || object.Equals(scalar, DBNull.Value))
            {
                return 1;
            }
            return int.Parse(scalar.ToString());
        }

        public static category GetModel(int id)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from categories ");
            builder.Append(" where id=" + id);
            category category = new category();
            Connection connection = new Connection();
            DataTable dataTable = connection.GetDataTable(builder.ToString());
            connection.Close();
            category.id = id;
            if (dataTable.Rows.Count > 0)
            {
                if (dataTable.Rows[0]["parentid"].ToString() != "")
                {
                    category.parentid = int.Parse(dataTable.Rows[0]["parentid"].ToString());
                }
                category.name = dataTable.Rows[0]["name"].ToString();
                category.description = dataTable.Rows[0]["description"].ToString();
                if (dataTable.Rows[0]["sortid"].ToString() != "")
                {
                    category.sortid = int.Parse(dataTable.Rows[0]["sortid"].ToString());
                }
                return category;
            }
            return null;
        }

        public static bool HasChilds(category[] cs, int id)
        {
            for (int i = 0; i < cs.Length; i++)
            {
                if (cs[i].parentid == id)
                {
                    return true;
                }
            }
            return false;
        }

        public static void Up(int id)
        {
            category model = GetModel(id);
            DataTable list = GetList("parentid=" + model.parentid.ToString() + " and sortid<" + model.sortid.ToString());
            if (list.Rows.Count >= 1)
            {
                category category2 = GetModel(int.Parse(list.Rows[list.Rows.Count - 1]["id"].ToString()));
                int sortid = model.sortid;
                model.sortid = category2.sortid;
                category2.sortid = sortid;
                Update(model);
                Update(category2);
            }
        }

        public static void Update(category model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update categories set ");
            builder.Append("[parentid]=" + model.parentid + ",");
            builder.Append("[name]='" + model.name + "',");
            builder.Append("[description]='" + model.description + "',");
            builder.Append("[sortid]=" + model.sortid + "");
            builder.Append(" where id=" + model.id + "");
            Connection connection = new Connection();
            connection.Execute(builder.ToString());
            connection.Close();
        }
    }


}
